package com.megurokai.command;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

import com.megurokai.paint.Status;

public class CommandBrush extends CommandPaint {
	private Path path;
	
	public CommandBrush(float x, float y) {
		super(x, y);
		
		path = null;
	}

	@Override
	public boolean draw(Status status) {
		Canvas canvas = status.getCanvas();
		Paint paint = status.getPaint();
		float fromX = status.getX();
		float fromY = status.getY();
		float toX = this.x;
		float toY = this.y;
		double r = status.getStrokeWidth()/2;
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL);

		if(path == null){
			path = new Path();
			
			double rad = Math.atan2(fromY-toY, fromX-toX);
			
			float cos = (float)(r*Math.cos(rad+Math.PI/2));
			float sin = (float)(r*Math.sin(rad+Math.PI/2));
			
			path.moveTo(fromX+cos, fromY+sin);
			path.lineTo(fromX-cos, fromY-sin);
			path.lineTo(toX-cos, toY-sin);
			path.lineTo(toX+cos, toY+sin);
			path.close();
		}
		
		canvas.drawPath(path, paint);
		canvas.drawCircle(toX, toY, (float)r, paint);
		
		status.setX(toX);
		status.setY(toY);
		return false;
	}
//	@Override
//	public boolean draw(Status status) {
//		Canvas canvas = status.getCanvas();
//		float fromX = status.getX();
//		float fromY = status.getY();
//		float toX = this.x;
//		float toY = this.y;
//		Paint paint = status.getPaint();
//		paint.setAntiAlias(true);
//		paint.setStyle(Paint.Style.FILL);
//		canvas.drawLine(fromX, fromY, toX, toY, paint);
//		double r = status.getStrokeWidth()/2;
//		double rad = Math.atan2(fromY-toY, fromX-toX);
//		
//		path.reset();
//		float cos = (float)(r*Math.cos(rad+Math.PI/2));
//		float sin = (float)(r*Math.sin(rad+Math.PI/2));
//		path.moveTo(fromX+cos, fromY+sin);
//		path.lineTo(fromX-cos, fromY-sin);
//		path.lineTo(toX-cos, toY-sin);
//		path.lineTo(toX+cos, toY+sin);
//		path.close();
//		canvas.drawPath(path, paint);
//		canvas.drawCircle(toX, toY, (float)r, paint);
//		
//		status.setX(toX);
//		status.setY(toY);
//		return false;
//	}
}
